<template>
	<view class="out-box">
		<view class="hader">
			<image class="sculpture" src="../../static/images/铺小多.png"></image>
			<view class="hear_title">Hi~微信用户</view>
		</view>
		<view class="main">
			<view class="main_top">欢迎成为小程序名称会员，请确认授权登录</view>
			<view class="main_cneter">获得您的公开信息（昵称、头像等）</view>
			<view class="main_empower">
				<view class="confirm" @tap="get_code()">授权登录</view>
				<view class="cancellation" @tap="im_back()">取消</view>
			</view>
		</view>
	</view>
</template>

<script setup>
import {ref,reactive,onBeforeMount,onMounted} from 'vue'
import {onGotApiData} from '../../util/api.js'
	const props = defineProps({
		obj: String,
	});
	console.log("sadasd",props.obj)

	// 在组件渲染之前执行
	onBeforeMount(() => {
		console.log('组件准备渲染',);
	})
	// 在组件渲染完成后执行
	onMounted(() => {
		console.log('组件已经渲染完毕',);
	});

	// 获取微信小程序的code
	let code_value = reactive({
		code:'',
		encryptedData:'',
		iv:''
	})
	const get_code =  () => {
		wx.login({
			success: function(res) {
				console.log('我是code',res.code);//这里的返回值里面便包含code
				code_value.code = res.code

				// 获取用户的头像和name
				wx.getUserInfo({
					success: function(res) {
						console.log('我是用户信息',res)
						code_value.encryptedData = res.encryptedData
						code_value.iv = res.iv 
						console.log('执行 get_open_id')
						// 获取open_id接口
						get_open_id()
					}
				})
			},
			fail: function(res) {
				console.log('登陆失败');
			},
			complete: function(res) {},
			})	
	}

	// 获取open_id
	const get_open_id = () =>{
			// 获取openid相关的变量方法
		let params = reactive({
			code : code_value.code,
			encryptedData : code_value.encryptedData,
			iv : code_value.iv
		})
		console.log("params",params)
		onGotApiData('/api/app/login/login','POST',params,open_id_result)
	}

	// 获取openid的结果
	const open_id_result = (result) => {
		console.log('result获取open_id',result)
		uni.setStorageSync('open_id', result.openid);
		uni.setStorageSync('user_id', result.userid);
		// 获取openid跳转到我的页面
		go_back()
	}
	// 跳转到我的店铺
	const go_back = () => {
		uni.switchTab({
			url: '/pages/my/my'
		});
	}

	// 授权取消
	const im_back = () => {
		uni.switchTab({
			url: '/pages/index/index'
		});
	}
</script>

<style lang="scss" scoped>
	.out-box {
		min-height: 100vh;
		width: 100vw;
		max-width: 750rpx;
		margin: 0 auto;
		overflow: hidden;
		font-family: '思源黑体';
		background-color: rgba(255, 255, 255, 1);
		width: 750rpx auto;
		.hader{
			width: 750rpx;
			height: 420rpx;
			margin:  0 auto;
			// border: 1px solid black;
			font-family: Arial, Helvetica, sans-serif;
			font-weight: 600;
			color: #F5F5F5;
			.sculpture{
				width: 160rpx;
				height: 160rpx;
				border-radius: 50%;
				margin: 130rpx 0 0 295rpx;
			}
			.hear_title{
				width: 750rpx;
				height: 50rpx;
				// background-color: pink;
				text-align: center;
				color: rgba(0, 0, 0, 1);
				font-family: '思源黑体';
				font-weight: 400;
				font-size: 30rpx;
				line-height: 50rpx;
			}
		}
		.main{
			width: 750rpx;
			height: 480rpx;
			margin:  0 auto;
			// background-color: pink;
			font-family: '思源黑体';
			color:rgba(0, 0, 0, 1);

			.main_top{
				font-weight: 700;
				font-size: 34rpx;
				text-indent: 40rpx;
			}
			.main_cneter{
				color: rgba(56, 56, 56, 1);
				font-size: 30rpx;
				margin-top: 10rpx;
				text-indent: 40rpx;
			}
			.main_empower{
				width:600rpx;
				height: 80rpx;
				margin: 80rpx 0 0 75rpx;
				// background-color: pink;s
				display: flex;
				justify-content: space-around;
				line-height: 70rpx;

				.confirm{
					width:200rpx;
					height: 70rpx;
					background-color: rgba(255, 87, 51, 1);
					text-align: center;
					border-radius: 14rpx;
					color: rgba(229, 229, 229, 1);
				}
				.cancellation{
					width: 200rpx;
					height: 70rpx;
					background-color: rgba(229, 229, 229, 1);
					text-align: center;
					border-radius: 14rpx;
				}

			}
		}
	}
</style>
